import { getUserInfo } from '@/api/user'
import { login } from '@/api/login'
import { clearLoginInfo, getToken, setToken } from '@/utils/auth'
import { localRead } from '@/utils'

export default {
  state: {
    userName: localRead('userInfo')?.username || '',
    nickname: localRead('userInfo')?.nickname || '',
    userId: localRead('userInfo')?.id || '',
    avatarImgPath: localRead('userInfo')?.avatar || '',
    token: getToken(),
    access: localRead('userInfo')?.access || '',
    hasGetInfo: false
  },
  mutations: {
    setAvatar(state, avatarPath) {
      state.avatarImgPath = avatarPath
    },
    setUserId(state, id) {
      state.userId = id
    },
    setUserName(state, name) {
      state.userName = name
    },
    setNickname(state, name) {
      state.nickname = name
    },
    setAccess(state, access) {
      state.access = access
    },
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setHasGetInfo(state, status) {
      state.hasGetInfo = status
    }
  },
  getters: {},
  actions: {
    // 登录
    handleLogin({ commit }, loginInfo) {
      return new Promise((resolve, reject) => {
        login({ ...loginInfo })
          .then((res) => {
            if (res && res.code === 200) {
              const data = res.data
              commit('setToken', data.token)
              localStorage.setItem('refreshToken', data.refreshToken)
              resolve(res)
            } else {
              this.$Message.error({
                background: true,
                content: res.message
              })
              // commit('setToken', '')
              resolve(res)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    // 退出登录
    handleLogOut({ state, commit }) {
      return new Promise((resolve, reject) => {
        // logout(state.token)
        //   .then(() => {
        //     commit('setToken', '')
        //     commit('setAccess', [])
        //     resolve()
        //   })
        //   .catch((err) => {
        //     reject(err)
        //   })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        commit('setToken', '')
        commit('setAccess', [])
        clearLoginInfo()
        resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo(state.token)
            .then((res) => {
              const data = res.data
              if (res && res.code === 200) {
                commit('setUserId', data.id)
                commit('setAvatar', data.avatar)
                commit('setUserName', data.username)
                commit('setNickname', data.nickname)
                commit('setAccess', data.access)
                commit('setHasGetInfo', true)
                resolve(res)
              } else {
                this.$Message.error({
                  background: true,
                  content: res.message
                })
                // resolve()
              }
            })
            .catch((err) => {
              reject(err)
            })
        } catch (error) {
          reject(error)
        }
      })
    }
  }
}
